package com.xea.service;

import java.util.List;

import com.uf.util.PageData;
import com.uf.util.SqlHelper;
import com.xea.model.Information;
public class InformationService {
	
	/**
	 * 查询家具常识
	 * @param name       标题名称
	 * @param topage     第几页
	 * @return
	 */
	public PageData search(String name,String topage){
		
		String sql = "select * from Information where title like '%"+name+"%' order by id";
		return SqlHelper.queryPageForMysql(sql, null, topage, null, Information.class);	
		
	}
	
	/**
	 * 查询家具常识公开
	 * @return
	 */
	public List<Information> searchTop(){
		
		String sql = "select a.* from (select * from Information order by createDate desc)a limit 0,6";
		return SqlHelper.query(sql, null, Information.class);
		
	}
	
	/**
	 * 新增家具常识
	 * @param news   家具常识实体
	 * @return
	 */
	public void add(Information information){
		
		String sql = "insert into Information(title,content,createDate,pub) values(?,?,?,?)";
		Object[] params=new Object[]{information.getTitle(),information.getContent(),information.getCreateDate(),information.getPub()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 家具常识详情
	 * @param id  id 
	 * @return
	 */
	public Information findById(int id){
		
		String sql = "select * from Information where id = ?";		
		List<Information> list = SqlHelper.query(sql, new Object[]{ id}, Information.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 删除家具常识
	 * @param id   id
	 * @return
	 */
	public void del(String id){
		
		String sql = "delete from Information where id in("+id+")";
		SqlHelper.excute(sql, null);
		
	}
	
	/**
	 * 修改是否公开
	 * @param id   id
	 * @param pub   是否公开
	 * @return
	 */
	public void pub(String id,int pub){
		
		String sql = "update Information set pub=? where id in("+id+")";
		Object[] params=new Object[]{pub};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 修改家具常识
	 * @param product   家具常识实体
	 * @return
	 */
	public void update(Information inforamtion){
		
		String sql = "update Information set title=?,content=?,pub=? where id=?";
		Object[] params=new Object[]{inforamtion.getTitle(),inforamtion.getContent(),inforamtion.getPub(),inforamtion.getId()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 查询家具常识
	 * @param name       标题名称
	 * @param topage     第几页
	 * @return
	 */
	public PageData searchforout(String topage){
		
		String sql = "select * from Information where pub=1 order by id";
		return SqlHelper.queryPageForMysql(sql, null, topage, "10", Information.class);	
		
	}
}
